package com.fau.wmbj.server.service.impl;

import com.fau.wmbj.server.entity.Order;
import com.fau.wmbj.server.entity.OrderMember;
import com.fau.wmbj.server.entity.Template;
import com.fau.wmbj.server.entity.User;
import com.fau.wmbj.server.service.OrderMemberService;
import com.fau.wmbj.server.service.OrderService;
import com.fau.wmbj.server.service.TemplateService;
import com.fau.wmbj.server.service.UserService;
import com.fau.wmbj.server.service.base.BaseServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 订单
 *
 * @author jiawei
 * @version 1.0
 * @datetime 2024-04-15 17:11
 */
@Service
public class OrderServiceImpl extends BaseServiceImpl<Order, Long> implements OrderService {

    @Resource
    private OrderMemberService orderMemberService;
    @Resource
    private UserService userService;
    @Resource
    private TemplateService templateService;

    @Override
    public Order save(Order order, User user) {
        List<OrderMember> orderMembers = new ArrayList<>();
        List<Map<String, Object>> o = (List<Map<String, Object>>) order.getParams().get("orderMembers");
        for (Map<String, Object> stringObjectMap : o) {
            OrderMember orderMember = new OrderMember();
            orderMember.setPurview((List<String>) stringObjectMap.get("purview"));
            orderMember.setUserId(Long.valueOf((Integer) stringObjectMap.get("userId")));
            orderMembers.add(orderMember);
        }
        orderMembers.addAll(order.getOrderMembers());
        order.setOrderMembers(new ArrayList<>());
        order.setCompany(user.getRole().getCompany());
        Long templateId = order.getTemplateId();
        Template template = templateService.find(templateId);
        order.setTableData(template.getTableData());
        Order save = super.save(order);
        for (OrderMember orderMember : orderMembers) {
            orderMember.setOrder_(save);
            User user1 = userService.find(orderMember.getUserId());
            orderMember.setUser(user1);
            orderMemberService.save(orderMember);
        }
        return save;
    }

    @Override
    public void update2(Order order, User user) {
        Order order1 = super.find(order.getId());
        Map<String, Object> tableData = order.getTableData();

        List<Map<String, Object>> body = (List<Map<String, Object>>) tableData.get("body");
        for (Map<String, Object> sheet : body) {
            Map<String, Object> config = (Map<String, Object>) sheet.get("config");
            config.put("colhidden", new HashMap<>());
            sheet.put("data", new ArrayList<>());
        }
        tableData.put("body", body);
        order1.setTableData(tableData);
        super.update(order1);
    }

    @Override
    public void update(Order order, User user) {
        Order order1 = super.find(order.getId());
        Long templateId = order1.getTemplateId();
        Template template = templateService.find(templateId);

        List<Map<String, Object>> body = order.getBody();
        Map<String, Object> tableData = order1.getTableData();

        List<Map<String, Object>> body1 = (List<Map<String, Object>>) tableData.get("body");
        Map<String, Object> sheet = body.get(0);
        Map<String, Object> sheet1 = body1.get(0);
        List<Map<String, Object>> new_celldata = (List<Map<String, Object>>) sheet.get("celldata");
        List<Map<String, Object>> old_celldata = (List<Map<String, Object>>) sheet1.get("celldata");
        List<Map<String, Object>> newcell_list = new ArrayList<>();
        List<Map<String, Object>> old_head_list = new ArrayList<>();
        List<Map<String, Object>> new_head_list = new ArrayList<>();

        for (int i = 0; i < old_celldata.size(); i++) {
            Map<String, Object> cell = old_celldata.get(i);
            Integer r = (Integer) cell.get("r");
            if (Objects.equals(r, template.getHeaderRow())) {
                new_head_list.add(cell);
            }
        }

        for (int i = 0; i < new_celldata.size(); i++) {
            Map<String, Object> cell = new_celldata.get(i);
            Integer r = (Integer) cell.get("r");
            Integer c = (Integer) cell.get("c");
            Map<String, Object> v = (Map<String, Object>) cell.get("v");
            if (Objects.equals(r, template.getHeaderRow())) {
                old_head_list.add(cell);
            } else if (r > template.getHeaderRow() && (v.get("v") != null || v.get("bg") != null)) {
                Map<String, Object> newcell = new HashMap<>();
                newcell.put("v", v);
                newcell.put("r", r);
                String hs = "";
                for (int j = 0; j < old_head_list.size(); j++) {
                    Map<String, Object> hcell = old_head_list.get(j);
                    Map<String, Object> hv = (Map<String, Object>) hcell.get("v");
                    Integer hc = (Integer) hcell.get("c");
                    if (Objects.equals(hc, c)) {
                        hs = (String) hv.get("v");
                        break;
                    }
                }
                newcell.put("h", hs);
                newcell_list.add(newcell);
            }
        }

        if (newcell_list.isEmpty()) {
            sheet1.put("celldata", new_celldata);
        } else {
            for (int i = 0; i < newcell_list.size(); i++) {
                Map<String, Object> new_cell = newcell_list.get(i);
                Map<String, Object> new_v = (Map<String, Object>) new_cell.get("v");
                Integer new_r = (Integer) new_cell.get("r");
                String new_h = (String) new_cell.get("h");
                boolean flag = false;
                for (int j = 0; j < old_celldata.size(); j++) {
                    Map<String, Object> old_cell = old_celldata.get(j);
                    Integer old_cell_r = (Integer) old_cell.get("r");
                    Integer old_cell_c = (Integer) old_cell.get("c");
                    String old_cell_h = "";
                    if (old_cell_r > template.getHeaderRow()) {
                        for (int n = 0; n < new_head_list.size(); n++) {
                            Map<String, Object> hcell = new_head_list.get(n);
                            Integer hc = (Integer) hcell.get("c");
                            Map<String, Object> hv = (Map<String, Object>) hcell.get("v");
                            if (Objects.equals(hc, old_cell_c)) {
                                old_cell_h = (String) hv.get("v");
                                break;
                            }
                        }
                    }
                    if (StringUtils.equals(old_cell_h, new_h) && Objects.equals(old_cell_r, new_r)) {
                        old_cell.put("v", new_v);
                        flag = true;
                        break;
                    }
                }
                if (!flag) {
                    Integer c = 0;
                    for (int n = 0; n < new_head_list.size(); n++) {
                        Map<String, Object> hcell = new_head_list.get(n);
                        Map<String, Object> hv = (Map<String, Object>) hcell.get("v");
                        if (Objects.equals((String) hv.get("v"), new_h)) {
                            c = (Integer) hcell.get("c");
                            break;
                        }
                    }
                    Map<String, Object> newcell = new HashMap<>();
                    newcell.put("v", new_v);
                    newcell.put("r", new_r);
                    newcell.put("c", c);
                    old_celldata.add(newcell);
                }
            }
            sheet1.put("celldata", old_celldata);
        }
        sheet1.put("config", sheet.get("config"));
        sheet1.put("data", new ArrayList<>());
        tableData.put("body",body1);
        order1.setTableData(tableData);
        order1.setOrderNo(order.getOrderNo());
        super.update(order1);
    }

    @Override
    public String calculate(Order order, User user) {
        Long id = order.getId();
        Order order1 = super.find(id);
        Template template = templateService.find(order1.getTemplateId());
        List<Map<String, Object>> body1 = order1.getBody();
        Map<String, Object> sheet1 = body1.get(0);
        Map<String, Object> params = order.getParams();
        List<Map<String, Object>> baseParameterList = template.getBaseParameter();
        List<Map<String, Object>> celldata_new = (List<Map<String, Object>>) params.get("celldata");
        Integer row = (Integer) params.get("row");
        Map<String, Object> values = new HashMap<>();
        List<Map<String, Object>> celldata = (List<Map<String, Object>>) sheet1.get("celldata");
        for (int i = 0; i < celldata.size(); i++) {
            Map<String, Object> cell = celldata.get(i);
            Integer r = (Integer) cell.get("r");
            Integer c = (Integer) cell.get("c");
            Map<String, Object> v = (Map<String, Object>) cell.get("v");
            if (template.getHeaderRow() == r) {
                Map<String, Object> head_v = (Map<String, Object>) cell.get("v");
                Object head_vv = head_v.get("v");
                if (head_vv != null) {
                    Map<String, Object> a = new HashMap<>();
                    a.put("head_name", (String) head_vv);
                    a.put("col_num", c);
                    a.put("sign", "A" + c);
                    values.put((Integer) head_v.get("c") + "", a);
                }
            }
            if (r == row) {
                Object a = values.get(c + "");
                if (a != null) {
                    Map<String, Object> am = (Map<String, Object>) a;
                    am.put("v", v.get("v"));
                }
            }
        }
        for (int i = 0; i < celldata_new.size(); i++) {
            Map<String, Object> cell = celldata_new.get(i);
            Integer c = (Integer) cell.get("c");
            Object v = cell.get("v");
            Object a = values.get(c + "");
            if (a != null) {
                Map<String, Object> am = (Map<String, Object>) a;
                am.put("val", v);
            }
        }
        for (int i = 0; i < baseParameterList.size(); i++) {
            Map<String, Object> bp = baseParameterList.get(i);
            String formula = (String) bp.get("formula");
            Integer index = (Integer) bp.get("index");
            Object v = values.get(index + "");
            if (v != null) {
                Map<String, Object> am = (Map<String, Object>) v;
                am.put("formula", formula);
            }
        }


        String val = "";

        return val;
    }

}
